package cn.slipi.admin.common.utils;


import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RouteUtil {

    public static List<String[]> allSorts = new ArrayList<>();

    public static List<String> doCalc(String[] points, String[] mustStartPoints, String[] notStartPoints, String[] mustEntPoints) {
        permutation(points, 0, points.length - 1);
        String[][] a = new String[allSorts.size()][];
        allSorts.toArray(a);
        List<String> pointLine = new ArrayList<>();
        for (String[] point : a) {
            List<String> pointList = new ArrayList<>(Arrays.asList(point));
            String pointJoin = Joiner.on("|").join(pointList);

            boolean mSLeft = mustStartPoints.length <= 0;
            for (String mustStartPoint : mustStartPoints) {
                if (pointJoin.startsWith(mustStartPoint)) {
                    mSLeft = true;
                    break;
                }
            }
            boolean nSLeft = true;
            if (mSLeft) {
                for (String notStartPoint : notStartPoints) {
                    if (pointJoin.startsWith(notStartPoint)) {
                        nSLeft = false;
                        break;
                    }
                }
            }
            boolean mELeft = mustEntPoints.length <= 0;
            if (nSLeft) {
                for (String mustEndPoint : mustEntPoints) {
                    if (pointJoin.endsWith(mustEndPoint)) {
                        mELeft = true;
                        break;
                    }
                }
            }
            if (mSLeft && nSLeft && mELeft) pointLine.add(pointJoin);
        }
        return pointLine;
    }

    private static void permutation(String[] nums, int start, int end) {
        if (start == end) {
            String[] newNums = new String[nums.length];
            if (end + 1 >= 0) System.arraycopy(nums, 0, newNums, 0, end + 1);
            allSorts.add(newNums);
        } else {
            for (int i = start; i <= end; i++) {
                String temp = nums[start];
                nums[start] = nums[i];
                nums[i] = temp;
                permutation(nums, start + 1, end);
                nums[i] = nums[start];
                nums[start] = temp;
            }
        }
    }
}
